<?php
class IndexAction extends Action {
	public function index() {
		//防止页面乱码  
		header ( "Content-Type:text/html; charset=utf-8" );
		$Show = M('Show');
		//$User = M('User');
		
		for($i=0;$i<4;$i++) {
			$j=$i+1;
			if(is_file('Public/Display/index/member/female0'.$j.'.jpg')) {
				$condition['show_type'] = '非缘莫属女';
				$condition['show_seq'] = $j;
				$female = $Show->where($condition)->find();
				$k=$j+4;
				$condition['show_seq'] = $k;
				$female2 = $Show->where($condition)->find();
				$femalelist[$i]['user_id'] = $female['show_url'];
				$femalelist[$i]['photo_id'] = 'female0'.$j;
				$femalelist[$i]['photo_url'] = '__PUBLIC__/Display/index/member/female0'.$j.'.jpg';
				$femalelist[$i]['user_id2'] = $female2['show_url'];
				$femalelist[$i]['photo_id2'] = 'female0'.$k;
				$femalelist[$i]['photo_url2'] = '__PUBLIC__/Display/index/member/female0'.$k.'.jpg';
			}
			if(is_file('Public/Display/index/member/male0'.$j.'.jpg')) {
				$condition['show_type'] = '非缘莫属男';	
				$condition['show_seq'] = $j;
				$male = $Show->where($condition)->find();
				$k=$j+4;
				$condition['show_seq'] = $k;
				$male2 = $Show->where($condition)->find();
				$malelist[$i]['user_id'] = $male['show_url'];
				$malelist[$i]['photo_id'] = 'male0'.$j;
				$malelist[$i]['photo_url'] = '__PUBLIC__/Display/index/member/male0'.$j.'.jpg';
				$malelist[$i]['user_id2'] = $male2['show_url'];
				$malelist[$i]['photo_id2'] = 'male0'.$k;
				$malelist[$i]['photo_url2'] = '__PUBLIC__/Display/index/member/male0'.$k.'.jpg';
			}	
		}
		
		$this->assign('femalelist',$femalelist);
		$this->assign('malelist',$malelist);
		$this->display ();
	}
	
	public function header() {
		//防止页面乱码  
		header ( "Content-Type:text/html; charset=utf-8" );
		$this->display ();
	}
	
	public function quicklogin() {
		//防止页面乱码  
		header ( "Content-Type:text/html; charset=utf-8" );
		
		$username = $_POST['quickUsername'];
		$userpwd = md5 ( $_POST['quickPwd'] );
		$User = M ('User');
		if(strstr($username,'@')) {
		 	$condition['user_email'] = $username;
		 	$condition['veri_email'] = '1';
		 	$condition['available'] = '1';
		}
		else if (preg_match('/^((13[0-9])|(15[0-9])|180|(18[5-9]))\d{8}$/',$username)) {		 		
		 	$condition['user_mobile'] = $username;
		 	$condition['veri_mobile'] = '1';
		 	$condition['available'] = '1';
		 }
		else {
			$condition['user_name'] = $username;
			$condition['available'] = '1';
		}	
		$result = $User->where($condition)->find();
		if (empty($result) || $result["user_password"] != $userpwd) {
			$this->ajaxReturn('用户名或密码错误！','',0);//ajax 用户名错误或者密码错误
		} else {
			setcookie(session_name(),session_id(),time()+1800,"/"); //设置session生存期
			session('user',$result);
			if($result['user_type']=='0') session('type','注册会员');
			else if($result['user_type']=='1') session('type','线下会员');
			else if($result['user_type']=='2') session('type','VIP会员');
			else session('type','贵宾会员');
			$data['last_login_time'] = "20".date('y-m-d H:i:s',time());
			$User->where($result)->save($data);
			//跳转会员密室
			$this->ajaxReturn ('','',1);
		}
	}
	
	public function navi() {
		//防止页面乱码  
		header ( "Content-Type:text/html; charset=utf-8" );
		$this->display ();
	}
	
	// 生成验证码  
	public function verifyImage() {
		import ( "ORG.Util.Image" );
		Image::buildImageVerify ();
	}
	
	public function reg() {
		//防止页面乱码  
		header ( "Content-Type:text/html; charset=utf-8" );
		if ($_POST['username']) {
			$User = M('User');
			
			$condition_n['user_name'] = $_POST['username'];
			$result_n = $User->where ($condition_n)->find();
			if(!empty($result_n)) $this->ajaxReturn('','',5);
			$condition_e['user_email'] = $_POST['user_email'];
			$result_e = $User->where ($condition_e)->find();
			if(!empty($result_e)) $this->ajaxReturn('','',6);
			$condition_m['user_mobile'] = $_POST['user_mobile'];
			$result_m = $User->where ($condition_m)->find();
			if(!empty($result_m)) $this->ajaxReturn('','',7);
			if (md5($_POST['verify']) != $_SESSION['verify']) $this->ajaxReturn('','',8); 
			
			
			$data['user_name'] = $_POST['username'];
			$data['user_email'] = $_POST['user_email'];
			$data['user_password'] = md5($_POST['user_password']);
			$data['user_nickname'] = $_POST['user_nickname'];
			$data['user_sex'] = $_POST['user_sex'];
			$data['user_birthday'] = $_POST['user_birthday'];
			$data['user_province'] = $_POST['user_province'];
			$data['user_city'] = $_POST['user_city'];
			$data['user_marriage'] = $_POST['user_marriage'];
			$data['user_income'] = $_POST['user_income'];
			$data['user_mobile'] = $_POST['user_mobile'];
			$data['reg_time'] = "20".date('y-m-d H:i:s',time());
			$data['last_login_time'] = "20".date('y-m-d H:i:s',time());
			$data['available'] = '1';
			$result = $User->add($data);
				
			$Detail = M('Detail');
			$Interest = M('Interest');
			$Work = M('Work');	
			$Mate = M('Mate');
			$condition_n['user_name'] = $_POST['username'];
			$data_d['real_name'] = $_POST['user_truename'];
			$data_d['qq'] = $_POST['user_qq'];
			$result_n = $User->where ($condition_n)->find();
			$data_d['user_id'] = $result_n['user_id'];
			$Detail->add($data_d);
			$data_i['user_id'] = $result_n['user_id'];
			$Interest->add($data_i);
			$data_w['user_id'] = $result_n['user_id'];
			$Work->add($data_w);
			$data_m['user_id'] = $result_n['user_id'];
			$Mate->add($data_m);
			
			setcookie(session_name(),session_id(),time()+1800,"/"); //设置session生存期
			session('user',$result_n['user_id']);
			$this->ajaxReturn('','',10);
		}
		else {
			$Opt = M('Option');
			$opt_condition['opt_flag'] = "province";
			$provincelist = $Opt->order('opt_id')->where($opt_condition)->select();
			$this->assign("provincelist",$provincelist);
			$this->display ();
		}
	}
	
	public function checkName() {
		//防止页面乱码  
		header ( "Content-Type:text/html; charset=utf-8" );
		$User = M ('User');
		$condition['user_name'] = $_POST['username'];
		$result = $User->where ($condition)->find();
		if(!$result) $this->ajaxReturn('','',1);
		else $this->ajaxReturn('','该用户名已经被注册！',0);
	}
	
	public function checkEmail() {
		//防止页面乱码  
		header ( "Content-Type:text/html; charset=utf-8" );
		$User = M ('User');
		$condition['user_email'] = $_POST['user_email'];
		$result = $User->where ($condition)->find();
		if(!$result) $this->ajaxReturn('','',2); 
		else $this->ajaxReturn('','该邮箱已经被注册！',0);
	}
	
	public function checkMobile() {
		//防止页面乱码  
		header ( "Content-Type:text/html; charset=utf-8" );
		$User = M ('User');
		$condition['user_mobile'] = $_POST['user_mobile'];
		$result = $User->where ($condition)->find();
		if(!$result) $this->ajaxReturn('','',3); 
		else $this->ajaxReturn('','该手机号码已经被注册！',0);
	}
	
	public function checkCode() {
		//防止页面乱码  
		header ( "Content-Type:text/html; charset=utf-8" );
		if (md5($_POST['code']) != $_SESSION['verify']) 
			$this->ajaxReturn('','验证码错误！',0); 
		$this->ajaxReturn('','',4);
	}
	
	public function login() {
		//防止页面乱码  
		header ( "Content-Type:text/html; charset=utf-8" );
		
		if ($_POST['username']) {
			$username = $_POST['username'];
			$userpwd = md5 ( $_POST['userpwd'] );
			$User = M ('User');
		 	if(strstr($username,'@')) {
		 		$condition['user_email'] = $username;
		 		$condition['veri_email'] = '1';
		 		$condition['available'] = '1';
		 	}
		 	elseif (preg_match('/^((13[0-9])|(15[0-9])|180|(18[5-9]))\d{8}$/',$username)) {		 		
		 		$condition['user_mobile'] = $username;
		 		$condition['veri_mobile'] = '1';
		 		$condition['available'] = '1';
		 	}
			else {
				$condition['user_name'] = $username;
				$condition['available'] = '1';
			}	
			$result = $User->where ($condition)->find();
			if (! $result || $result ["user_password"] != $userpwd) {
				$this->ajaxReturn('','*账户名或密码错误！',0);//ajax 用户名错误或者密码错误
			} else {
				setcookie(session_name(),session_id(),time()+1800,"/"); //设置session生存期
				session('user',$result);
				if($result['user_type']=='0') session('type','注册会员');
				else if($result['user_type']=='1') session('type','线下会员');
				else if($result['user_type']=='2') session('type','VIP会员');
				else session('type','贵宾会员');
				$data['last_login_time'] = "20".date('y-m-d H:i:s',time());
				$User->where($result)->save($data);
				//跳转会员密室
				$this->ajaxReturn ('','',1);
			}
		}
		else $this->display();
	}
	
	public function logout() {
		//防止页面乱码  
		header ( "Content-Type:text/html; charset=utf-8" );
		
		session('[destroy]');
		$this-> redirect('Index:index');
		
	}
	
	public function send() {
		session('user',1); //test
		//防止页面乱码  
		header ( "Content-Type:text/html; charset=utf-8" );
		$User = M('User'); 
		$condition['user_id'] = $_SESSION['user'];
		$info = $User->where($condition)->find();
		$code = md5($info['user_id'].$info['user_name'].$info['user_email']);
		
		$email = $info['user_email'];
		$title = '天生嫁对网用户注册邮箱验证';  //标题
		$content = '亲爱的'.$info['user_name'].', 您好！
		感谢您加入天生嫁对婚恋网，我们将为您提供最优质的婚恋交友服务！
		请点击下面的链接完成天生嫁对婚恋网用户注册邮箱验证。
		http://localhost/svn/UrbanLove/index.php/Index/verify?id='.$info['user_id'].'&code='.$code.'
		如果浏览器不能自动打开，请把地址复制到浏览器中手动打开。';  //邮件内容
		if(SendMail($email,$title,$content)) { //直接调用发送即可
			$this->success('发送成功', 'login');
		} else {
			$this->error('发送失败,请重试！');
		}
	}
	
	public function verify() {
		//防止页面乱码  
		header ( "Content-Type:text/html; charset=utf-8" );
		$User = M('User'); 
		$condition['user_id'] = $_GET['id'];
		$info = $User->where($condition)->find();
		$code = md5($info['user_id'].$info['user_name'].$info['user_email']);
		if($code==$_GET['code']) {
			print("验证成功！");
			$data['veri_email'] = '1';
			$User->where($condition)->save($data);
		} else {
			print("验证失败！");
		}
	}
}